public class Solution4 {
    public static void main(String[] args) {
        String num1 = "123";
        String num2 = "456";
        System.out.println(multiply(num1, num2));
    }
    public static String multiply(String num1, String num2) {
        int m = num1.length();
        int n = num2.length();
        int[] tmp = new int[m + n - 1];
        StringBuffer ret = new StringBuffer();
        String num_1 = new StringBuffer(num1).reverse().toString();
        String num_2 = new StringBuffer(num2).reverse().toString();
        // 无进位相加
        for(int i = 0; i < n; i++) {
            int n1 = num_2.charAt(i) - '0';
            for(int j = 0; j < m; j++) {
                if(n1 == 0) {
                    continue;
                }
                int n2 = num_1.charAt(j) - '0';
                tmp[i + j] += n1 * n2;
            }
        }
        // 处理进位
        int num = 0;
        for(int i = 0; i < tmp.length; i++) {
            num += tmp[i];
            ret.append((char)('0' + (char)(num % 10)));
            num /= 10;
        }
        while(num != 0) {
            ret.append((char)('0' + (char)(num % 10)));
            num /= 10;
        }
        // 处理前导0
        while(ret.length() > 1 && ret.charAt(ret.length() - 1) == '0') {
            ret.deleteCharAt(ret.length() - 1);
        }
        ret.reverse();
        return ret.toString();
    }
}
